package com.pi.dc.service;

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import com.pi.base.util.bean.ObjectTool;
import com.pi.comm.query.BaseParam;
import com.pi.dc.dao.entity.DcCmdLogEntity;
import com.pi.dc.dao.entity.DcDeviceInfoEntity;
import com.pi.dc.dao.mapper.DcCmdLogMapper;
import com.pi.dc.dao.param.DcCmdLogParam;
import com.pi.dc.enumerate.CmdForwordEnum;
import com.pi.dc.enumerate.CmdPhaseEnum;
import com.pi.dc.enumerate.CmdTypeEnum;
import com.pi.dc.model.DcLogPostForm;

/**
 * @description 日志记录类
 * @author sub
 *
 */
@Validated
@Service
public class SmartLogService {
  
  private static final Logger logger = LoggerFactory.getLogger(SmartLogService.class);
  @Autowired
  private DcDeviceInfoService devInfoService;
  @Autowired
  private DcCmdLogMapper cmdLogMapper;
  
  public void addSmartLog(@Valid DcLogPostForm form){
    DcCmdLogEntity entity = new DcCmdLogEntity();
    entity.setCmdId(form.getCmdId());
    entity.setDeviceCode(form.getDevCode());
    entity.setRawData(form.getRawData());
    if(null != form.getSourceId()) {
      entity.setSourceId(form.getSourceId());
    }else {
      DcDeviceInfoEntity devInfo = devInfoService.getByDevCodeIfExist(entity.getDeviceCode());
      entity.setSourceId(null != devInfo ? devInfo.getSourceId(): 0);
    }
    DcCmdLogEntity dcCmdLog = getDcCmdLog(form.getCmdId(), form.getDevCode());
    if(null == dcCmdLog) {
      cmdLogMapper.insert(entity);
    }else {
      entity.setId(dcCmdLog.getId());
      cmdLogMapper.updateById(entity);
    }
  }
  
  public DcCmdLogEntity getDcCmdLog(Long cmdId, String devCode) {
    if(null == cmdId) {
      logger.debug("要查询的设备Id为空： {}", cmdId);
      return null;
    }
    DcCmdLogParam param = new DcCmdLogParam();
    param.setCmdId(cmdId);
    param.setDeviceCode(devCode);
    param.setOrderParam("update_date");
    param.setOrderOperator(BaseParam.ORDER_TYPE_DESC);
    return ObjectTool.getOne(cmdLogMapper.findList(param));
  }
  
  public List<DcCmdLogEntity> getWaitDistributedCmdByType(@NotBlank(message = "DEV_CODE_EMPTY") String devCode, @NotNull(message="CMD_TYPE_EMPTY") CmdTypeEnum cmdTypeEnum) {
      DcCmdLogParam param = new DcCmdLogParam();
      param.setDeviceCode(devCode);
      param.setCmdType(cmdTypeEnum.getType());
      param.setCmdForword(CmdForwordEnum.DISTRIBUTED.getType());
      param.setOrderParam("update_date");
      param.setOrderOperator(BaseParam.ORDER_TYPE_DESC);
      return cmdLogMapper.findList(param);
  }
  
  public void refreshCmdSendStatus(List<Long> cmdIds) {
      if(CollectionUtils.isEmpty(cmdIds)) {
          return;
      }
      //暂时没有批量的，先这样吧
      for(Long id: cmdIds) {
          DcCmdLogEntity dcCmdLog = new DcCmdLogEntity();
          dcCmdLog.setId(id);
          dcCmdLog.setCmdPhase(CmdPhaseEnum.NORMAL.getPhase());
          cmdLogMapper.updateById(dcCmdLog); 
      }
  }
}
